package org.dao.imp;

import java.util.List;

import org.dao.LiuyanDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.pojo.Liuyan;
import org.pojo.User;
import org.util.HibernateSessionFactory;

public class LiuyanDaoImp implements LiuyanDao {

	private Session session;
	private Transaction ts;
	
	public LiuyanDaoImp() {
		super();
		session = HibernateSessionFactory.getSession();
		ts = session.beginTransaction();
	}

	public boolean addLiuyan(Liuyan newLiuyan) {
		session.save(newLiuyan);
		ts.commit();
		return false;
	}

	public List<Liuyan> findAllLiuyan() {
		Query query = session.createQuery("from Liuyan");
		List<Liuyan> list = query.list();
		return list;
	}
	//查询留言信息
	public Liuyan findLiuyan(int id) {
		Query query = session.createQuery("from Liuyan where id=?");
		query.setParameter(1, id);
		List<Liuyan> list =  query.list();
		Liuyan liuyan = list.get(0);
		return liuyan;
	}
	//更新留言信息
	public boolean updateLiuyan(Liuyan existLiuyan) {
		session.update(existLiuyan);
		ts.commit();
		return true;
	}

	public boolean deleteLiuyan(Liuyan delLiuyan) {
		session.delete(delLiuyan);
		return true;
	}

	public List<User> findAllUser() {
		Query query = session.createQuery("from User");
		List<User> list = query.list();
		return list;
	}

}
